'use strict';
const Koa = require('koa');
// 引入路由文件
const router = require('./router/index');
//body-parser: 该中间件用于post请求的数据
const bodyParser = require('koa-bodyparser');

// 连接数据库
const db = require('./db/db');

const app = new Koa();

// 解析请求体中的参数
app.use(bodyParser());

// 使用koa-router中间件
app.use(router.routes()).use(router.allowedMethods());
/**
 *   后续记得处理log中间件
 * */

// 接口访问权限设置
app.use(async (ctx, next) => {
  ctx.response.set('Access-Control-Allow-Origin', 'http://localhost:3000');
  // ctx.response.set('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization');
  ctx.response.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  await next()
});

app.listen(3000, () => {
  console.log(`cashbook server started on http://localhost:3000, current env is ${process.env.NODE_ENV}`);
});
